package com.cy.device.config;

import java.text.*;
import java.util.Date;

/**
 * @author Administrator
 */
public class TmsDateFormat extends DateFormat {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private DateFormat dateFormat;// Jackson原有的格式化

    // 自定义格式化
    private SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat formatOne = new SimpleDateFormat("yyyy-MM");
    private SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat formatThree = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private SimpleDateFormat formatFour = new SimpleDateFormat("yyyy年MM月dd日");
    private SimpleDateFormat formatFive = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
    private SimpleDateFormat formatSix = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

    public TmsDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return defaultFormat.format(date, toAppendTo, fieldPosition);
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        Date date = null;
        try {
            if (source.matches("^\\d{4}-\\d{1,2}$")) {
                date = formatOne.parse(source, pos);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
                date = formatTwo.parse(source, pos);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                date = formatThree.parse(source, pos);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                date = defaultFormat.parse(source, pos);
            } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日$")) {
                date = formatFour.parse(source, pos);
            } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}时\\d{1,2}分$")) {
                date = formatFive.parse(source, pos);
            } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}时\\d{1,2}分\\d{1,2}秒$")) {
                date = formatSix.parse(source, pos);
            } else {
                throw new IllegalArgumentException("Invalid date value '" + source + "'");
            }
        } catch (Exception e) {
            date = dateFormat.parse(source, pos);
        }
        return date;
    }

    // 主要还是装饰这个方法
    @Override
    public Date parse(String source) throws ParseException {
        Date date = null;
        try {
            if (source.matches("^\\d{4}-\\d{1,2}$")) {
                date = formatOne.parse(source);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
                date = formatTwo.parse(source);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                date = formatThree.parse(source);
            } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                date = defaultFormat.parse(source);
            } else {
                throw new IllegalArgumentException("Invalid date value '" + source + "'");
            }
        } catch (Exception e) {
            date = dateFormat.parse(source);
        }
        return date;
    }

    // 这里装饰clone方法的原因是因为clone方法在jackson中也有用到
    @Override
    public Object clone() {
        Object format = dateFormat.clone();
        return new TmsDateFormat((DateFormat) format);
    }
}